﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Plugins;
using Plugins.Core;

namespace StockAnalyst2.Plugins.Engine
{
	internal class PluginLoader
	{
		#region Events

		public event EventHandler<PluginAttachRequestedEventArgs> PluginAttachRequested;

		#endregion

		#region Public Methods

		public void Load(IEnumerable<IStrategyPlugin> plugins)
		{
			foreach (IStrategyPlugin plugin in plugins)
			{
				this.OnPluginAttachRequested(this.CreatePluginMenuEntry(plugin), this.CreateToolBarMenuEntry(plugin), plugin);
			}
		}

		protected virtual void OnPluginAttachRequested(MenuItem menuItem, Button toolBarButton, IStrategyPlugin plugin)
		{
			if (this.PluginAttachRequested != null)
			{
				this.PluginAttachRequested(this, new PluginAttachRequestedEventArgs(menuItem, toolBarButton, plugin));
			}
		}

		#endregion

		#region Private Methods

		// <MenuItem Header="Sample Strategy" x:Name="StrategyMenu" Command="{Binding ApplicationCommandsVM.SelectPluginCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Mode=Self}}" />
		private MenuItem CreatePluginMenuEntry(IStrategyPlugin plugin)
		{
			MenuItem result = new MenuItem();

			result.Tag = plugin;
			result.Header = plugin.VisualRepresentation.MenuText;

			Binding commandBinding = new Binding("ApplicationCommandsVM.SelectPluginCommand");
			BindingOperations.SetBinding(result, MenuItem.CommandProperty, commandBinding);

			Binding commandParameterBinding = new Binding("Tag");
			commandParameterBinding.RelativeSource = RelativeSource.Self;
			BindingOperations.SetBinding(result, MenuItem.CommandParameterProperty, commandParameterBinding);

			return result;
		}

		//<Button Command="{Binding ApplicationCommandsVM.SelectPluginCommand}" CommandParameter="{Binding Tag, RelativeSource={RelativeSource Mode=Self}}" x:Name="StrategyToolBar" >
		//    <Button.Style>
		//        <Style>
		//            <Style.Triggers>
		//                <Trigger Property="ToggleButton.IsEnabled" Value="False">
		//                    <Setter Property="ToggleButton.Opacity" Value="0.5" />
		//                </Trigger>
		//            </Style.Triggers>
		//        </Style>
		//    </Button.Style>
		//    <Image Source="Resources\ToolBar.ico" />
		//    <Button.ToolTip>
		//        <ToolTip>
		//            <StackPanel>
		//                <TextBlock FontWeight="Bold" Foreground="Black" Text="Sample Strategy" />
		//                <TextBlock Foreground="Black" Text="View the sample strategy plugin." />
		//            </StackPanel>
		//        </ToolTip>
		//    </Button.ToolTip>
		//</Button>
		private Button CreateToolBarMenuEntry(IStrategyPlugin plugin)
		{
			Button result = new Button();

			result.Tag = plugin;

			Binding commandBinding = new Binding("ApplicationCommandsVM.SelectPluginCommand");
			BindingOperations.SetBinding(result, MenuItem.CommandProperty, commandBinding);

			Binding commandParameterBinding = new Binding("Tag");
			commandParameterBinding.RelativeSource = RelativeSource.Self;
			BindingOperations.SetBinding(result, MenuItem.CommandParameterProperty, commandParameterBinding);

			result.Content = new Image() { Source = plugin.VisualRepresentation.ToolBarIcon };

			Style buttonStyle = new Style();
			Trigger enabledTrigger = new Trigger();
			enabledTrigger.Property = UIElement.IsEnabledProperty;
			enabledTrigger.Value = false;
			enabledTrigger.Setters.Add(new Setter(UIElement.OpacityProperty, 0.5));
			buttonStyle.Triggers.Add(enabledTrigger);

			result.Style = buttonStyle;

			StackPanel toolTip = new StackPanel();
			TextBlock toolTipCaption = new TextBlock();
			toolTipCaption.Foreground = Brushes.Black;
			toolTipCaption.FontWeight = FontWeights.Bold;
			toolTipCaption.Text = plugin.VisualRepresentation.MenuText;

			TextBlock toolTipBody = new TextBlock();
			toolTipBody.Foreground = Brushes.Black;
			toolTipBody.Text = "Shows the plugin controller window.";

			toolTip.Children.Add(toolTipCaption);
			toolTip.Children.Add(toolTipBody);

			result.ToolTip = toolTip;

			return result;
		}

		#endregion

	}
}
